package com.changgou.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.DeleteListenPoint;
import com.xpand.starter.canal.annotation.InsertListenPoint;
import com.xpand.starter.canal.annotation.UpdateListenPoint;

import java.util.List;

/**
 * @ClassName CanalDataEventListener
 * @Description 监听器：通过canal服务监控数据
 * @Author 传智播客
 * @Date 12:40 2019/11/14
 * @Version 2.1
 **/
@CanalEventListener
public class CanalDataEventListener {

    /**
     * 添加数据
     * @param entryType  监听操作数据库的事件
     * @param rowData    获取的行数据
     */
    @InsertListenPoint
    public void insertEvent(CanalEntry.EntryType entryType, CanalEntry.RowData rowData){
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList){
            String columnNanme = column.getName();
            String columnValue = column.getValue();
            System.out.println("列名：" + columnNanme + "--列值：" + columnValue);
        }
    }

    /**
     * 更新数据
     * @param entryType 监听操作数据库的事件
     * @param rowData   获取行的数据
     */
    @UpdateListenPoint
    public void updateEvent(CanalEntry.EntryType entryType , CanalEntry.RowData rowData){
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList){
            String columnName = column.getName();
            String columnValue = column.getValue();
            System.out.println("列名：" + columnName + "---列值：" + columnValue);
        }
        System.out.println("-----------------更新前和后的数据-----------------");
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList){
            String columnName = column.getName();
            String columnValue = column.getValue();
            System.out.println("列名：" + columnName + "---列值：" + columnValue);
        }
    }

    /**
     * 删除
     * @param entryType 监听操作数据库的事件
     * @param rowData   获取行的数据
     */
    @DeleteListenPoint
    public void deleteEven(CanalEntry.EntryType entryType , CanalEntry.RowData rowData){
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList){
            String columnName = column.getName();
            String columnValue = column.getValue();
            System.out.println("列名：" + columnName + "--列值：" + columnValue);
        }
    }
}
